package com.dcbeida.activity.addnewpatient.historyofsmoking;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.adapter.HospitalOfSmokingAdapter;
import com.dcbeida.bean.HistoryOfSmokingBean;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 吸烟史
 * Created by swt on 2017/9/6.
 */

public class HistoryOfSmoking extends BaseActivity implements HistoryOfSmokingContract.View {
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.myList)
    ListView myList;
    private String xysYear = "1年";
    private int itemIndex = 0;
    private String xys;
    private HistoryOfSmokingPresenter presenter;
    private HospitalOfSmokingAdapter adapter;
    private int index;

    @Override
    protected int getLayoutResId() {
        return R.layout.history_of_smoking2;
    }

    @Override
    public void setData() {
        super.setData();
        imgBack.setVisibility(View.VISIBLE);
        tvTitle.setText(R.string.xys);
        tvRight.setText(R.string.bc);

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            xys = bundle.getString("xys");
            itemIndex = bundle.getInt("itemIndex");
            index = bundle.getInt("index");
        }

        presenter = new HistoryOfSmokingPresenter(this, this);
        presenter.getData("1", "0", "10");

        adapter = new HospitalOfSmokingAdapter(HistoryOfSmoking.this);
        myList.setAdapter(adapter);
        myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setonItemClick(position, view);
            }
        });
        adapter.index = index;
        adapter.itemIndex = itemIndex;

    }

    @OnClick({R.id.img_back, R.id.tv_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_right:

                if (adapter.index != -1) {
                    HistoryOfSmokingBean.ListBean bean = (HistoryOfSmokingBean.ListBean) adapter.getItem(adapter.index);

                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();

                    bundle.putString("xys", bean.getName());
                    if (bean.getId() != 4) {
                        bundle.putString("xysYear", adapter.xysYear);
                        bundle.putInt("itemIndex", adapter.itemIndex);
                    } else {
                        //不吸烟
                        bundle.putString("xysYear", "");
                        bundle.putInt("itemIndex", 0);
                    }
                    bundle.putInt("index", adapter.index);
                    bundle.putSerializable("HistoryOfSmokingBean", bean);
                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);
                }

                finish();
                break;

        }
    }


    @Override
    public void setPresenter(HistoryOfSmokingContract.Present presenter) {

    }

    @Override
    public void getDataError(String msg, int type) {
        showTip(msg);
    }

    @Override
    public void getDataSuccess(HistoryOfSmokingBean historyOfSmokingBean) {
        ArrayList<HistoryOfSmokingBean.ListBean> listBeen = historyOfSmokingBean.getList();
        adapter.setmArrayList(listBeen);
    }

    @Override
    public void showTip(String msg) {
        super.showTip(msg);
    }


}
